package AppView;

import Listeners.CanvasListeners;
import RuntimeInfo.State;
import ShapeModel.*;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class CanvasSection extends JPanel {


    public CanvasSection() {
        setLayout(null); // 设置为绝对布局方法

        setBackground(Color.LIGHT_GRAY);
        // mouse和mousemotion都要添加监听器
        addMouseListener(CanvasListeners.getCanvasMouseListener());
        addMouseMotionListener(CanvasListeners.getCanvasMouseMotionListener());
    }

    private void useAuxiliarySetting(Graphics2D g2d){
        g2d.setColor(Color.BLACK);
        g2d.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_ROUND, 3.5f, new float[] { 5, 2 }, 0f));
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(State.curColor);

        for(CADShape shape:State.shapeList){
            shape.setDrawingConfig(g2d);
            // 获取边框
            Rectangle2D bound = shape.getBounds2D();

            if(shape instanceof CADText){
                CADText ts = (CADText) shape;
                g2d.setFont(ts.getFont());
                // 二者比例： 字母0.75，汉字1.25
                g2d.drawString(ts.getText(),(int)shape.getX(),(int)(bound.getHeight()/1.25)+(int)shape.getY());

            }
            else{
                g2d.draw(shape);
            }

            if(shape== State.current){// 为当前选中的图形绘画辅助边框
                useAuxiliarySetting(g2d);// 将画笔修改为绘画辅助边框的设置
                if(shape instanceof CADRectangle){// 如果画矩形，那么辅助边框稍微画大一点，凸显出选中聚焦
                    bound.setRect(bound.getMinX()-2,bound.getMinY()-2,bound.getWidth()+4,bound.getHeight()+4);
                }
                g2d.draw(bound);// 绘画辅助边框
            }
        }
        g2d.dispose();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }
}




